const baseURL = 'http://localhost:3000';

const request = (url, method = 'GET', data = {}) => {
  return new Promise((resolve, reject) => {
    wx.request({
      url: baseURL + url,
      method,
      data,
      header: { 'Content-Type': 'application/json' },
      success: (res) => {
        if (res.statusCode === 200 && res.data && res.data.code === 200) {
          resolve(res.data);
        } else {
          reject((res.data && res.data.message) || `请求失败(${res.statusCode})`);
        }
      },
      fail: () => reject('网络错误，请检查后端服务或网络连接')
    });
  });
};

module.exports = { request };